/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-02-17
*/
package com.rzico.basics.controller;

import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Member;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.MemberService;
import com.rzico.core.entity.SysPlugin;
import com.rzico.core.plugin.AuthPlugin;
import com.rzico.core.service.SysPluginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 产品档案控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-02-17
 */
@Api(description = "企业信息接口")
@RestController
@RequestMapping("/enterprise")
public class EnterpriseController extends BaseController {

    @Autowired
    private MemberService memberService;
    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private SysPluginService sysPluginService;

    /**
     * 获取当前企业资料
     *
     * @return
     */
    @ApiOperation("获取当前企业资料")
    @GetMapping("/getInfo")
    @ApiImplicitParams({@ApiImplicitParam(name = "mchId", value = "商户号", dataType = "String", paramType = "query")
    })
    public CommResult<Enterprise> getInfo(String mchId) {
        if (mchId==null) {
            Member member = memberService.getCurrent();
            if (member != null) {
                mchId = member.getMchId();
            }
        }
        Enterprise result = enterpriseService.findByMch(mchId);
        return CommResult.success(result);

    }

    /**
     * 获取服务号二维码
     *
     * @return
     */
    @ApiOperation("获取服务号关注二维码")
    @GetMapping("/getQrcode")
    @ApiImplicitParams({@ApiImplicitParam(name = "mchId", value = "商户号", dataType = "String", paramType = "query")
    })
    public CommResult<Enterprise> getQrcode(String mchId) {
        Enterprise result = enterpriseService.findByMch(mchId);
        Map<String,Object> data = new HashMap<>();
        if (result.getQrcode()==null) {
            SysPlugin sysPlugin = sysPluginService.findByPlugin(result.getMchId(),"weixinOAuth2Plugin");

            if (sysPlugin==null) {
                return CommResult.error("没有安装插件");
            }
            AuthPlugin oAuth2Plugin = sysPluginService.getAuthPlugin("weixinOAuth2Plugin");

            Map<String,String> q = oAuth2Plugin.createQrcode(sysPlugin,"0000");
            if (q.get("return_code").equals("0000")) {
                data.put("qrcode",q.get("qrcode"));
            } else {
                return CommResult.error("无效二维码");
            }

        } else {
            data.put("qrcode",result.getQrcode());
        }
        return CommResult.success(data);

    }


}
